﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Skomart.Update.Download;

namespace Skomart.Update.Forms
{
    public partial class UpdateForm : Form
    {
        private UpdateManager updateManager;
        private string systemName;
        private Thread thread;

        public UpdateForm()
        {
            InitializeComponent();
            systemName = ConfigurationManager.AppSettings["systemName"];
            this.Load += new EventHandler(UpdateForm_Load);
            updateManager = new UpdateManager();
            updateManager.DownloadStateChanged += new DownloadStateChangedHandler(updateManager_DownloadStateChanged);
            updateManager.UpdateCompleted += new EventHandler(updateManager_UpdateCompleted);
        }

        void updateManager_UpdateCompleted(object sender, EventArgs e)
        {
            Application.Exit();   
        }

        private void updateManager_DownloadStateChanged(object sender, DownloadStateEventArgs eventArgs)
        {
            SetStatusLable(eventArgs.Discription);
            SetCurrentFileLable(eventArgs.CurrentFileName);
            SetFileProressBar(eventArgs.FileDownLoadPercent);
            SetTotalProressBar(eventArgs.AllDownloadPercent);
        }

        private delegate void SetTextCallBack(string text);

        private delegate void SetProgressBarCallBack(int value);

        private void SetStatusLable(string text)
        {
            if (lblStatus.InvokeRequired)
            {
                SetTextCallBack callBack = new SetTextCallBack(SetStatusLable);
                this.Invoke(callBack, new object[] { text });
            }
            else
            {
                this.lblStatus.Text = text;
            }
        }

        private void SetCurrentFileLable(string text)
        {
            if (lblCurrentFile.InvokeRequired)
            {
                SetTextCallBack callBack = new SetTextCallBack(SetCurrentFileLable);
                this.Invoke(callBack, new object[] { text });
            }
            else
            {
                this.lblCurrentFile.Text = text;
            }
        }

        private void SetTotalProressBar(int totalProgress)
        {
            if (pressbarTotal.InvokeRequired)
            {
                SetProgressBarCallBack callBack = new SetProgressBarCallBack(SetTotalProressBar);
                this.Invoke(callBack, new object[] { totalProgress });
            }
            else
            {
                this.pressbarTotal.Value = totalProgress;
            }
        }

        private void SetFileProressBar(int fileProgress)
        {
            if (pressbarFile.InvokeRequired)
            {
                SetProgressBarCallBack callBack = new SetProgressBarCallBack(SetFileProressBar);
                this.Invoke(callBack, new object[] { fileProgress });
            }
            else
            {
                this.pressbarFile.Value = fileProgress;
            }
        }

        void UpdateForm_Load(object sender, EventArgs e)
        {
            this.Text = systemName + "更新";
            bool hasUpdate = updateManager.HasNewUpdate();
            if (hasUpdate)
            {
                UpdateAlertForm alertForm = new UpdateAlertForm();
                this.Hide();
                DialogResult dialogResult = alertForm.ShowDialog(this);
                if (dialogResult == DialogResult.OK)
                {
                    this.Show();
                    thread = new Thread(new ThreadStart(updateManager.Update));
                    thread.Start();
                }
                else
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }
    }
}
